/**
 * Copyright (C) 2010 DustedPixels.com
 */
package com.dustedpixels.cirt.model.machine.spectrum;

import com.dustedpixels.cirt.model.core.Circuit;
import com.dustedpixels.cirt.model.core.Script;
import com.dustedpixels.cirt.model.machine.spectrum.rom.ByteRam;
import com.dustedpixels.cirt.model.machine.spectrum.ula.Ula;
import com.dustedpixels.cirt.model.video.ByteVideo;

/**
 * ZxSpectrum48
 *
 * @author Michal Pociecha-Los (micapolos@gmail.com)
 */
@Circuit
public final class ZxSpectrum48 {

  @Circuit private final Ula ula;
  @Circuit private final ByteRam rom;
  @Circuit private final ByteRam ram1;
  @Circuit private final ByteRam ram2;
  @Circuit private final ByteVideo video;
  
  public ZxSpectrum48(byte[] romBytes, byte[] ram1Bytes, byte[] ram2Bytes, byte[] videoBytes) {
    ula = new Ula();
    rom = new ByteRam(romBytes, (1 << 14) - 1);
    ram1 = new ByteRam(ram1Bytes, (1 << 14) - 1);
    ram2 = new ByteRam(ram2Bytes, (1 << 15) - 1);
    video = new ByteVideo(videoBytes, 352, 304, 448);
  }
  
  @Script
  public void update() {
    // Update ULA
    ula.update();

    // Update Video from ULA
    video.blank = ula.blank;
    video.hSync = ula.hSync;
    video.vSync = ula.vSync;
    video.color = ula.color;
    video.update();
    
    // Update memory from ULA
    if (ula.read) {
      ram1.address = ula.address;
      ram1.read();
      ula.data = ram1.data;
    }    
  }
}
